<template>
  <div class="search-table-page">
    <Row type="flex"
         justify="space-between"
         class="row-operation">
      <i-col span="18">
        <search-form @search="handleSearch"></search-form>
      </i-col>
      <i-col span="4"
             class="col-right">
        <Button type="primary"
                icon="md-add"
                class="button-add"
                style="margin-right: 5px;"
                @click="addGoods">新增</Button>
        <Poptip confirm
                title="确定删除？"
                @on-ok="okdelete">
          <Button icon="md-trash"
                  type="error">删除</Button>
        </Poptip>
      </i-col>
    </Row>
    <table-page :loading="loading"
                :columns1="columns1"
                :data1="tableData"
                :noData="noData"
                :currentPage="currentPage"
                :totalPage="totalPage"
                :pageSize="pageSize"
                @multi-select="multiSelect"
                @change-page="changePage"
                @change-page-size="changPageSize">
    </table-page>
  </div>
</template>

<script>
import add from './add/add'
import edit from './edit/edit'
import searchForm from './search-form/search-form'
import tablePage from '_c/table-page/table-page'
import { getGoodsList, delGoods } from '@/api/goods'

export default {
  name: 'goods-list',
  components: {
    add,
    edit,
    searchForm,
    tablePage
  },
  data () {
    return {
      exportLoading: false,
      loading: false,
      goAbled: false,
      columns1: [
        {
          type: 'selection',
          width: 50,
          align: 'center'
        },
        {
          type: 'index',
          width: 40,
          align: 'center'
        },
        {
          title: '商品名称',
          key: 'ProductName',
          tooltip: true,
          align: 'center'
        },
        {
          title: '总销售量',
          key: 'TotalSalesNum'
        },
        {
          title: '原价',
          key: 'OriginalPrice'
        },
        {
          title: '现价',
          key: 'PresentPrice'
        },
        {
          title: '品牌',
          key: 'Brand'
        },
        {
          title: '商品类型',
          key: 'ProductType'
        },
        {
          title: '折旧',
          key: 'Depreciation',
          render: (h, params) => {
            const row = params.row
            const text = row.Depreciation === 0 ? '全新' : '二手'
            return h('span', {}, text)
          }
        },
        {
          title: '商品排序',
          key: 'Sort'
        },
        {
          title: '推荐',
          key: 'RecommendatType',
          render: (h, params) => {
            const row = params.row
            const text = row.RecommendatType === 0 ? '不推荐' : '推荐'
            return h('span', {}, text)
          }
        },
        {
          title: '爆款推荐',
          key: 'IsHotProduct',
          render: (h, params) => {
            const row = params.row
            const text = row.IsHotProduct === 0 ? '不推荐' : '推荐'
            return h('span', {}, text)
          }
        },
        {
          title: '今日推荐',
          key: 'IsTodayProduct',
          render: (h, params) => {
            const row = params.row
            const text = row.IsTodayProduct === 0 ? '不推荐' : '推荐'
            return h('span', {}, text)
          }
        },
        {
          title: '审核状态',
          key: 'State',
          render: (h, params) => {
            const row = params.row
            const text = row.State === 0 ? '未通过' : '已通过'
            return h('span', {}, text)
          }
        },
        {
          title: '操作',
          key: 'handle',
          width: 80,
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h('Button', {
                props: {
                  type: 'primary',
                  size: 'small'
                },
                on: {
                  click: () => {
                    this.$router.push({
                      path: '/goods_manage/goods_list/edit_goods',
                      query: {
                        id: params.row.ProductID
                      }
                    })
                  }
                }
              }, '编辑')
            ])
          }
        }
      ],
      noData: '暂无数据',
      currentPage: 1,
      totalPage: 0,
      pageSize: 10,
      tableData: [],
      selectedIds: [],
      searchData: {
        'PageIndex': '1',
        'PageSize': '10',
        'Search': []
      }
    }
  },
  mounted () {
    this.init()
    this.getTableData()
  },
  methods: {
    init () {
      this.searchData = {
        'PageIndex': '1',
        'PageSize': '10',
        'Search': []
      }
    },
    // 获取表格数据信息
    getTableData (parameter) {
      this.loading = true
      parameter = this.searchData
      getGoodsList(parameter).then(res => {
        if (res.data.success) {
          // console.log(res.data.data.aaData)
          this.totalPage = res.data.data.TotalCount
          this.currentPage = res.data.data.CurrentPageIndex
          this.tableData = res.data.data.aaData
          this.loading = false
        } else {
          this.$Message.info(res.data.msg || '缺少必要参数！')
          this.loading = true
        }
      }).catch(err => {
        console.log(err)
      })
    },
    // 页码切换
    changePage (page) {
      this.currentPage = page
      this.searchData.PageIndex = page
      this.getTableData()
    },
    // 切换每页显示多少条数据
    changPageSize (pageSize) {
      this.init()
      this.pageSize = pageSize
      this.searchData.PageSize = pageSize
      this.getTableData()
    },
    handleSearch (searchContent) {
      this.searchData.Search = searchContent
      this.getTableData(this.searchData)
    },
    // 多选
    multiSelect (selection) {
      this.selectedIds = selection.map(item => item.Id)
    },
    okdelete () {
      if (this.selectedIds.length === 0) {
        this.$Message.info('请选择要删除的用户！')
      } else {
        const delData = {
          SysUserId: localStorage.getItem('userId'),
          Ids: this.selectedIds
        }
        delGoods(delData).then(res => {
          if (res.data.success) {
            this.$Message.info(res.data.msg)
          } else {
            this.$Message.info(res.data.msg)
          }
        }).then(() => {
          this.getTableData()
        })
      }
    },
    // 新增商品
    addGoods () {
      this.$router.push({
        path: '/goods_manage/goods_list/add_goods'
      })
    }
  }
}
</script>

<style lang="less" scoped>
.search-table-page {
  padding: 20px;
  min-height: 85vh;
  background-color: #fff;
  .row-operation {
    margin-bottom: 20px;
    .col-right {
      text-align: right;
      .spteen {
        margin-left: 5px;
      }
    }
  }
}
</style>
